import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-04-06
 * Time: 18:30
 */
public class Test {
    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }
    /**
     * 增强对比度
     *
     * @param src BGR格式图像
     * @return
     */
    public static Mat histEqualize(Mat src) {
        Mat dst = src.clone();
        Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2YCrCb);
        List<Mat> list1 = new ArrayList<>();
        Core.split(dst, list1);
        Imgproc.equalizeHist(list1.get(0), list1.get(0));
        //Imgproc.equalizeHist(list1.get(1), list1.get(1));
        //Imgproc.equalizeHist(list1.get(2), list1.get(2));
        Core.normalize(list1.get(0), list1.get(0), 0, 255, Core.NORM_MINMAX);
        //Core.normalize(list1.get(1), list1.get(1), 0, 255, Core.NORM_MINMAX);
        //Core.normalize(list1.get(2), list1.get(2), 0, 255, Core.NORM_MINMAX);
        Core.merge(list1, dst);
        Imgproc.cvtColor(dst, dst, Imgproc.COLOR_YCrCb2BGR);
        return dst;
    }
    /**
     * 用于整体偏暗图像的增强,变亮
     *
     * @param src
     * @return
     */
    public static Mat laplaceEnhance(Mat src) {
        Mat srcClone = src.clone();
//        float[] kernel = {0, 0, 0, -1, 5f, -1, 0, 0, 0};
        float[] kernel = {0, 0, 0, -1, 5f, -1, 0, 0, 0};
        Mat kernelMat = new Mat(3, 3, CvType.CV_32FC1);
        kernelMat.put(0, 0, kernel);
        Imgproc.filter2D(srcClone, srcClone, CvType.CV_8UC3, kernelMat);
        return srcClone;
    }
    public static void main(String[] args) {
        Mat src = Imgcodecs.imread("src2.png");
        HighGui.imshow("oip",src);
        HighGui.waitKey(0);

        Mat dst = histEqualize(src);
        HighGui.imshow("change",dst);
        HighGui.waitKey(0);

//        Mat mat = laplaceEnhance(src);
//        HighGui.imshow("laplace",mat);
//        HighGui.waitKey(0);
//        Mat dst = new Mat();
//        Mat usm = new Mat();
//        Imgproc.GaussianBlur(mat, dst, new Size(0, 0), 100, 100);
//        Core.addWeighted(mat, 1.5, dst, -0.5, 0, usm);
//        Imgcodecs.imwrite("usm.jpg", usm);
//        HighGui.imshow("usm", usm);
//        HighGui.waitKey();
    }

        public static void main1(String[] args) {
            System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
            Mat m  = Mat.eye(3, 3, CvType.CV_8UC1);
            Mat m1  = Mat.zeros(3, 3, CvType.CV_8UC1);
            Mat m2 = Mat.ones(3, 3, CvType.CV_8UC1);
            System.out.println(m.dump());
            System.out.println(m1.dump());
            System.out.println(m2.dump());
        }


}
